home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V1 CW9 / 68K and PPC Projects (CW9) / 7.04 - SoundMaker / SoundMaker.c < prev    next >
C/C++ Source or Header  |  1996-06-01  |  3KB  |  124 lines

  1. /********************************************************/
  2. /*                                                        */
  3. /*  SoundMaker Code from Chapter Seven of                */
  4. /*                                                        */
  5. /*    *** The Macintosh Programming Primer, 2nd Ed. ***    */
  6. /*                                                      */
  7. /*    Copyright 1992, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /********************************************************/
  10.  
  11. #include <Sound.h>
  12. #include <SoundInput.h>
  13. #include <GestaltEqu.h>
  14.  
  15. #define kBaseResID            128
  16.  
  17. #define    kNilSoundChannel    nil
  18. #define    kSynchronous        false
  19.  
  20. #define    kEmptyString        "\p"
  21. #define kNilFilterProc        nil
  22.  
  23. #define    kErrorAlertID        kBaseResID
  24.  
  25.  
  26. /***************/
  27. /*  Functions  */
  28. /***************/
  29.  
  30. void    ToolBoxInit( void );
  31. Handle    RecordSound( void );
  32. void    PlaySound( Handle soundHandle );
  33. void    DoError( Str255 errorString, Boolean fatal );
  34.  
  35.  
  36. /**************************** main **********************/
  37.  
  38. void    main( void )
  39. {
  40.     Handle    soundHandle;
  41.     long    feature;
  42.     OSErr    err;
  43.     
  44.     MaxApplZone();
  45.     ToolBoxInit();
  46.     
  47.     err = Gestalt( gestaltSoundAttr, &feature );
  48.     
  49.     if ( err != noErr )
  50.         DoError( "\pError returned by Gestalt!", true );
  51.         
  52.     if ( feature & (1 << gestaltHasSoundInputDevice) )
  53.     {
  54.         soundHandle = RecordSound();
  55.         PlaySound( soundHandle );
  56.         DisposHandle( soundHandle );
  57.     }
  58.     else
  59.         DoError( "\pSound input device not available!!!", true );
  60. }
  61.  
  62.  
  63. /****************** ToolBoxInit *********************/
  64.  
  65. void    ToolBoxInit( void )
  66. {
  67.     InitGraf( &qd.thePort );
  68.     InitFonts();
  69.     InitWindows();
  70.     InitMenus();
  71.     TEInit();
  72.     InitDialogs( 0L );
  73.     InitCursor();
  74. }
  75.  
  76.  
  77. /****************** RecordSound ***********************/
  78.  
  79. Handle    RecordSound( void )
  80. {
  81.     OSErr        err;
  82.     Point        upperLeft;
  83.     Handle        soundHandle;
  84.     
  85.     SetPt( &upperLeft, 50, 50 );
  86.     
  87.     soundHandle = nil;
  88.         
  89.     err = SndRecord( nil, upperLeft, (OSType)siBestQuality, (SndListHandle *)(&soundHandle) );
  90.         
  91.     if ( err == userCanceledErr )
  92.         DoError( "\pRecording canceled...", true );
  93.     
  94.     if ( err != 0 )
  95.         DoError( "\pError returned by SndRecord()...", true );
  96.     
  97.     return( soundHandle );
  98. }
  99.  
  100.  
  101. /****************** PlaySound ***********************/
  102.  
  103. void    PlaySound( Handle soundHandle )
  104. {
  105.     OSErr    err;
  106.     
  107.     err = SndPlay( kNilSoundChannel, (SndListHandle)soundHandle, kSynchronous );
  108.     
  109.     if ( err != noErr )
  110.         DoError( "\pError returned by SndPlay()...", true );
  111. }
  112.  
  113.  
  114. /***************** DoError ********************/
  115.  
  116. void    DoError( Str255 errorString, Boolean fatal )
  117. {
  118.     ParamText( errorString, kEmptyString, kEmptyString, kEmptyString );
  119.     
  120.     StopAlert( kErrorAlertID, kNilFilterProc );
  121.     
  122.     if ( fatal )
  123.         ExitToShell();
  124. }